class Solution {
public:
/*
  / 递归
    int binarysearch(vector<int>& nums, int target, int left, int right){
        if(left > right) return -1;
        int mid = left + (right-left)/2;
        if(nums[mid] == target) return mid;
        else if(nums[mid] < target)
             left = mid+1;  // 说明target在右边区间
        else if(nums[mid] > target)
            right = mid-1; // 说明target在左边区间
        return binarysearch(nums, target, left, right);

    }
    int search(vector<int>& nums, int target) {
        return binarysearch(nums, target, 0, nums.size()-1);
    }
*/
    int search(vector<int>& nums, int target) {
        int left = 0, right = nums.size()-1;
        while(left <= right){
            int mid = left+((right-left)>>1);
            if(nums[mid] == target)
                return mid;
            else if(nums[mid] < target)
                left = mid + 1;
            else 
                right = mid - 1;
        }
        return -1;
    }
}; 